MainingPanel = Ext.extend(Ext.TabPanel, {
	initComponent : function() {
		MainingPanel.superclass.initComponent.call(this);
	},
	openTab : function(id,text){
		var n = this.getComponent(id);
		if (n) {
			this.setActiveTab(n);
		}else{
			var c = {
					'id' : id,
					'title' : text,
					closable : true
				};
			var pn = this.findPanel(id);
			if(!pn){
			pn = Ext.apply(c, {
					layout : 'border',
					items : [os.listPanel(),os.gridPanel(),os.shopsForm()]
				});
			}
			n = this.add(pn);
			this.setActiveTab(n);
		}
		
	},
	
	loadTab : function(node) {
		var n = this.getComponent(node.id);
		if (n) {
			this.setActiveTab(n);
		} else {
			var c = {
				'id' : node.id,
				'title' : node.text,
				closable : true
			};
			
			
			
			var pn = this.findPanel(node.id);
			if(node.id == "clientManager"){
				cstore.load({params : {start : 0, limit : 15}});
				if(!pn){
					pn = Ext.apply(c, {
					layout : 'border',
					items : [renders.grid()]
				});
			}
			n = this.add(pn);
			
			}else if(node.id == "user"){
				 userStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [user.userGrid(),user.userForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "shopping"){
				listStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [listSL.listGrid(),listSL.grid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "pack"){
				packingListStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [packings.listPanel(),packings.packingsForm(),packings.grid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "pk"){
				pkListStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [pks.listPanel(),pks.contractsForm(),pks.grid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "bvApply"){
				bvListStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [bvs.listPanel(),bvs.bvsForm(),bvs.grid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "balance"){
				balanceStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [balance.balanceForm(),balance.balanceGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "cnca"){
				cncaStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [cnca.cncaForm(),cnca.cncaGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "contract"){
				contractListStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [contracts.listPanel(),contracts.contractsForm(),contracts.grid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "cmc"){
				cmcListStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [cmcs.listPanel(),cmcs.grid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "createOrder"){
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [invoices.listPanel(),invoices.invoicesForm(),invoices.grid()]
					});
				}
				n = this.add(pn);
				invoiceListStore.load({params : {start : 0, limit : 6}});
			}else if(node.id == "createShopping"){
				
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [cs.listPanel(),cs.grid(),cs.shopsForm()]
					});
				}
				n = this.add(pn);
				csListStore.load({params : {start : 0, limit : 6}});
			}else if(node.id == "warehousing"){
				depotStore.load({params : {start : 0, limit : 6}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [depots.listPanel(),depots.gridPanel(),depots.goodsForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "storage"){
				storageStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [storages.storageGrid(),storages.storageForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "picking"){
				pickListStore.load({params : {start : 0, limit : 6}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [picks.listPanel(),picks.grid(),picks.picksForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "sale"){
				saleListStore.load({params : {start : 0, limit : 6}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [sales.listPanel(),sales.grid(),sales.salesForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "transfer"){
				transferListStore.load({params : {start : 0, limit : 6}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [transfers.listPanel(),transfers.grid(),transfers.transfersForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "inventory"){
				inventoryListStore.load({params : {start : 0, limit : 6}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [inventorys.listPanel(),inventorys.grid(),inventorys.inventorysForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lFee"){
				feeListStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [fees.feeForm(),fees.feeGrid(),fees.feeListGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "oFee"){
				ofeeListStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [ofees.feeForm(),ofees.feeGrid(),ofees.feeListGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lTax"){
				localTaxListStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [localTaxs.localTaxForm(),localTaxs.localTaxListGrid(),localTaxs.localTaxGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "oTax"){
				overseaTaxListStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [overseaTaxs.overseaTaxForm(),overseaTaxs.overseaTaxListGrid(),overseaTaxs.overseaTaxGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "sdRecei"){
				sdreceivableStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [sdreceivables.receivableForm(),sdreceivables.receivableGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lRecei"){
				receivableStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [receivables.receivableForm(),receivables.receivableGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "oRecei"){
				oreceiStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [oreceis.oreceiForm(),oreceis.oreceiGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "sdPay"){
				sdshouldPayStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [sdshouldPays.shouldPayForm(),sdshouldPays.shouldPayGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lSpay"){
				shouldPayStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [shouldPays.shouldPayForm(),shouldPays.shouldPayGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "oSpay"){
				osPayStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [osPays.osPayForm(),osPays.osPayGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "runcost"){
				runCostStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [runCosts.runCostForm(),runCosts.runCostGrid(),runCosts.rcGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "selfH"){
				selfListStore.load({params : {start : 0, limit : 12}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [selfHarvests.selfHarvestForm(),selfHarvests.selfHarvestGrid(),selfHarvests.selfListGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "actH"){
				actHarvestStore.load({params : {start : 0, limit : 12}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [actHarvests.actHarvestForm(),actHarvests.actHarvestGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "otherH"){
				otherHarvestStore.load({params : {start : 0, limit : 12}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [otherHarvests.otherHarvestForm(),otherHarvests.otherHarvestGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "harvest"){
				harvestStore.load({params : {start : 0, limit : 13}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [harvests.harvestGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "track"){
				invoiceTrackStore.load({params : {start : 0, limit : 5}});
				existentInvoiceStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [invoiceTracks.invoiceTrackGrid(),invoiceTracks.invoiceTrackForm(),invoiceTracks.listPanel()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lSell"){
				sellListStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [sellLists.sellListGrid(),sellLists.sellListForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "oSell"){
				osellListStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [osellLists.osellListGrid(),osellLists.osellListForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lfund"){
				fundStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [funds.fundGrid(),funds.fundForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lfRecei"){
				fundReceiStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [fundReceis.fundReceiGrid(),fundReceis.fundReceiForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lfPay"){
				shallPayStore.load({params : {start : 0, limit : 5}});
				fundPayStore.load({params : {start : 0, limit : 8}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [fundPays.shallPayGrid(),fundPays.fundPayGrid(),fundPays.fundPayForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lshallpay"){
				shallPayStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [shallPays.shallPayGrid(),shallPays.shallPayForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "lshallcei"){
				shallCeiStore.load({params : {start : 0, limit : 15}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [shallCeis.shallCeiGrid(),shallCeis.shallCeiForm()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "stat"){
				statStore.load({params : {start : 0, limit : 10}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [stats.listForm(),stats.listGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "unSubmit"){
				unSubmitStore.load({params : {start : 0, limit : 5}});
				EIStore.load({params : {start : 0, limit : 6}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [unSubmits.listPanel(),unSubmits.unSubmitsForm(),unSubmits.grid(),unSubmits.EIGrid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "unDb"){
				unDbStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [unDbs.listPanel(),unDbs.unDbsForm(),unDbs.grid()]
					});
				}
				n = this.add(pn);
			}else if(node.id == "drawBack"){
				unDrawBackStore.load({params : {start : 0, limit : 5}});
				drawBackStore.load({params : {start : 0, limit : 5}});
				if(!pn){
				pn = Ext.apply(c, {
						layout : 'border',
						items : [drawBacks.drawBackGrid(),drawBacks.drawBackForm(),drawBacks.grid()]
					});
				}
				n = this.add(pn);
			}else{
				n = this.add(pn ? new pn(c) : Ext.apply(c, {
					html : '你还没有实现该页面！'
				}))
			}
		//限制tab的数量
			if(this.items.getCount()>2){
				this.remove(this.items.get(1)) ;
			}
			n.show().doLayout();
		}
	
	},
	findPanel : function(name) {
		var ret;
			
			var pn = (this.ns ? this.ns : 'MainingPanel') + "."
					+ Ext.util.Format.capitalize(name) + 'Panel';
			
			var ret = eval(pn);
		return ret;
	}
});
